$(function () {

    getUserInof();


    let layer = layui.layer
    $("#btnLogout").on('click', function () {

        layer.confirm('是否确认退出?', { icon: 3, title: '提示' }, function (index) {
            //do something

            localStorage.removeItem('token')
            location.href = "/login.html";
            layer.close(index);
        });
    })
});
function getUserInof() {

    $.ajax({
        url: "/my/userinfo",
        // headers：属性，用来设置请求头信息
        // headers: {
        // Authorization：属性用户身份验证
        //     Authorization: localStorage.getItem
        //         ('token') || ""
        // },
        success: function (res) {
            // console.log(res)
            if (res.status != 0) {
                return layui.layer.msg(res.message)
            }
            renderAvatar(res.data)
        }
    })
}

function renderAvatar(user) {
    let name = user.nickname || user.username;

    $("#welcome").html('欢迎&nbsp;' + name)
    if (user.user_pic !== null) {

        $(".layui-nav-img").show().attr('src', user.user_pic);
        $(".text-avatar").hide()
    } else {
        let text = name[0].toUpperCase()
        $(".text-avatar").show().html(text)
        $(".layui-nav-img").hide();
    }

}
